package com.huangj.pro2_h2pop;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.widget.Toast;

import com.huangj.fragments.HomeFragment;
import com.huangj.fragments.MineFragment;
import com.huangj.fragments.ScheduleFragment;
import com.huangj.fragments.TravelsFragment;
import com.huangj.huangjlibrary.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements TabLayout.OnTabSelectedListener {

    @BindView(R.id.vp_home)
    public ViewPager viewPager;
    @BindView(R.id.tabLayouts)
    public TabLayout tablayout;

    private
    HomeViewPagerAdapter homeViewPagerAdapter;
    private int [] pics={R.drawable.icon_tab_home,
                            R.drawable.icon_tab_trip,
                            R.drawable.icon_tab_plan,
                            R.drawable.icon_tab_my};
    private String []tabs= {"攻略","游记","行程单","我的"};

    @Override
    protected int getContentId() {
        return R.layout.activity_home;
    }
    /**
     * 初始化；
     */
    @Override
    protected void init() {

        homeViewPagerAdapter = new HomeViewPagerAdapter(getSupportFragmentManager());
        homeViewPagerAdapter.addFragment(new HomeFragment(),tabs[0]);
        homeViewPagerAdapter.addFragment(new TravelsFragment(),tabs[1]);
        homeViewPagerAdapter.addFragment(new ScheduleFragment(),tabs[2]);
        homeViewPagerAdapter.addFragment(new MineFragment(),tabs[3]);
        viewPager.setAdapter(homeViewPagerAdapter);
        tablayout.setupWithViewPager(viewPager);//设置联动；
        tablayout.setOnTabSelectedListener(this);//设置TabLayout的监听；
        tablayout.getTabAt(0).setIcon(R.drawable.icon_tab_home);
        tablayout.getTabAt(0).setText("");
        tablayout.getTabAt(0).select();
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        tab.setText("");
        tab.setIcon(pics[tab.getPosition()]);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        tab.setText(tabs[tab.getPosition()]);
        tab.setIcon(null);
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }


    /**
     * ViewPagerd的适配器；
     */
    public static class HomeViewPagerAdapter extends FragmentStatePagerAdapter{

        private List<Fragment> mhomeFragments;
        private List<String> tabs;


            public HomeViewPagerAdapter(FragmentManager fm) {
            super(fm);
            mhomeFragments = new ArrayList<>();
            tabs = new ArrayList<>();
        }

        public void addFragment(Fragment fragment,String tab){
            mhomeFragments.add(fragment);
            tabs.add(tab);
        }
        @Override
        public Fragment getItem(int position) {
            return mhomeFragments.get(position);
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return tabs.get(position);
        }
        @Override
        public int getCount() {
            return tabs.size();
        }
    }

    /**
     * 双击退出；
     */
    private long end;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (System.currentTimeMillis() - end <= 2000){
                this.finish();
            }else{
                Toast.makeText(MainActivity.this, "再次点击退出氢气球", Toast.LENGTH_SHORT).show();
                end = System.currentTimeMillis();
            }
        }
        return false;
    }

}
